window.operateEvents = {
    'click .detail': function (e, value) {
        window.wxc.xcConfirm(value, window.wxc.xcConfirm.typeEnum.info);
    }
};

var log_bar_chart;

$(function () {
    get_pie_chart();
});

function get_pie_chart() {
    $.ajax({
        url: 'servlet/LogBarChartServlet',
        type: 'post',
        dataType: 'json',
        success: function (json) {
            draw_log_bar_chart(json);
        },
        error: function () {
            window.wxc.xcConfirm("生成堆积图失败", window.wxc.xcConfirm.typeEnum.error);
        }
    })
}

function draw_log_bar_chart(data) {
    var bg_image = new Image();
    bg_image.src = 'img/background.jpg';
    var data_series = [];

    for (var key in data) {
        data_series.push({
            value: data[key],
            name: key
        })
    }

    var itemStyle = {
        normal: {
            opacity: 0.7,
            color: {
                image: bg_image,
                repeat: 'repeat'
            },
            borderWidth: 3,
            borderColor: '#235894'
        }
    };
    var option = {
        title: {
            text: '日志饼图',
            textStyle: {
                color: '#235894'
            }
        },
        tooltip: {},
        series: [{
            name: '异常种类',
            type: 'pie',
            selectedMode: 'single',
            selectedOffset: 30,
            clockwise: true,
            label: {
                normal: {
                    textStyle: {
                        fontSize: 18,
                        color: '#235894'
                    }
                }
            },
            labelLine: {
                normal: {
                    lineStyle: {
                        color: '#235894'
                    }
                }
            },
            data: data_series,
            itemStyle: itemStyle
        }]
    };
    echarts.dispose(document.getElementById('pie_chart'));
    log_bar_chart = echarts.init(document.getElementById('pie_chart'));
    log_bar_chart.setOption(option, true, false);
}

function query_log() {
    var start_time = $('#log_date_start').val();
    var end_time = $('#log_date_end').val();

    if (start_time === "" || end_time === "") {
        window.wxc.xcConfirm("选择时间段", window.wxc.xcConfirm.typeEnum.error);
        return;
    }
    var columns = init_column();
    var $table = $('#log-table');
    $table.bootstrapTable('destroy');
    $table.bootstrapTable({
        url: "servlet/LogQueryServlet",
        method: 'get',
        sortOrder: 'desc',
        cache: false,
        pagination: true,
        sidePagination: 'server',
        pageNumber: 1,
        pageSize: 10,
        pageList: [10, 20, 30],
        columns: columns,
        showRefresh: true,
        showToggle: true,
        showColumns: true,
        showExport: true,
        queryParams: function (params) {
            return {
                pageSize: params.limit,
                pageNumber: params.offset,
                sortOrder: params.order,
                start: start_time,
                end: end_time
            }
        },
        onLoadSuccess: function (json) {
            console.log(json);
        },
        onLoadError: function () {
            window.wxc.xcConfirm("数据查询失败", window.wxc.xcConfirm.typeEnum.error);
        }
    });
}

function init_column() {
    var columns = [];
    columns.push({
        field: 'id',
        title: '编号'
    });
    columns.push({
        field: 'operator',
        title: '操作人'
    });
    columns.push({
        field: 'time',
        title: '时间'
    });
    columns.push({
        field: 'type',
        title: '类型'
    });
    columns.push({
        field: 'level',
        title: '等级'
    });
    columns.push({
        field: 'information',
        title: '详细信息',
        formatter: detail_button(),
        events: 'operateEvents'
    });
    return columns;
}

function detail_button() {
    return '<button type="button" class="detail btn btn-info">查看</button>';
}